﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Designtime.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Designer.Views
{
    public static class DeleteThread
    {
        private static bool s_isRunning = false;
        private static Thread s_thread;
        private static Action<Guid, string, int, int> s_progress;
        private static Action<bool, int, int> s_completed;
        private static List<SolutionTreeViewNode> s_nodeList;

        /// <summary>
        /// solutiontreeviewNode状态在线程中修改
        /// </summary>
        /// <param name="nodeList"></param>
        /// <param name="progress">项目ID, 项目名称，签入进度，总数量</param>
        /// <param name="completed">是否出现错误，成功数量，总数量</param>
        public static bool Start(List<SolutionTreeViewNode> nodeList, Action<Guid, string, int, int> progress, Action<bool, int, int> completed)
        {
            if (s_isRunning)
            {
                return false;
            }

            s_nodeList = nodeList;
            s_progress = progress;
            s_completed = completed;
            s_isRunning = true;
            s_thread = new Thread(Running);
            s_thread.Start();
            return true;
        }

        public static void Stop()
        {
            if (s_isRunning)
            {
                s_thread.Abort();
                s_isRunning = false;
                s_thread = null;
            }
        }

        private static void Running()
        {
            int total = s_nodeList.Count;
            int progress = 0;
            bool hasError = false;

            CPPEI.Coolzon.ServiceProxy.DesignServiceRef.DesignServiceClient dsClient =
                CPPEI.Coolzon.ServiceProxy.DesignServiceProxyManager.Create();

            string workspacePath = UIContext.Current.LocalWorkSpace.LocalPath;
            Guid solutionId = UIContext.Current.CurrentSolution.ID;


            for (int index = 0; index < s_nodeList.Count; index++)
            {
                SolutionTreeViewNode tvNode = s_nodeList[index];

                CPPEI.Coolzon.ServiceProxy.DesignServiceRef.DeleteRequest dRequest = new ServiceProxy.DesignServiceRef.DeleteRequest();
                dRequest.Token = UIContext.Current.Token;
                dRequest.Id = tvNode.ID;
                dRequest.SolutionId = solutionId;
                dRequest.WorkSpaceId = UIContext.Current.LocalWorkSpace.Id;

                ServiceProxy.DesignServiceRef.ServiceResult result = dsClient.Delete(dRequest);
                //删除成功
                if (result.IsSuccess)
                {
                    tvNode.NodeStatus = NodeStatus.Delete;
                    switch (tvNode.NodeType)
                    {
                        case NodeType.Scene:
                            UIContext.Current.CurrentSolution.RemoveScene(SolutionHelper.GetScene(UIContext.Current.CurrentSolution, tvNode.ID));
                            break;
                        case NodeType.Folder:
                            UIContext.Current.CurrentSolution.RemoveFolder(SolutionHelper.GetFolder(UIContext.Current.CurrentSolution, tvNode.ID));
                            break;
                        case NodeType.Resource:
                            UIContext.Current.CurrentSolution.RemoveResource(SolutionHelper.GetResource(UIContext.Current.CurrentSolution, tvNode.ID));
                            break;
                        case NodeType.Template:
                            UIContext.Current.CurrentSolution.RemoveTemplate(SolutionHelper.GetTemplate(UIContext.Current.CurrentSolution, tvNode.ParentID, tvNode.ID));
                            LocalStorage.SaveSceneFile(workspacePath, UIContext.Current.CurrentSolution.ID, SolutionHelper.GetScene(UIContext.Current.CurrentSolution, tvNode.ParentID));
                            break;
                        case NodeType.StaticDataSource:
                            StaticDataSource sd = SolutionHelper.GetStaticDataSource(UIContext.Current.CurrentSolution, tvNode.ID);
                            UIContext.Current.CurrentSolution.RemoveStaticDataSource(sd);
                            break;
                    }

                    LocalStorage.SaveSolutionFile(workspacePath, UIContext.Current.CurrentSolution);
                    VSFileMgr.GoToDelete(workspacePath, solutionId, tvNode.ID);
                    progress++;
                }
                else
                {
                    hasError = true;
                    System.Windows.Application.Current.Dispatcher.Invoke(() => { Coolzon.UI.CustomMessageBox.ShowError(result.Message); });
                    Logger.ErrorToTag(Library.LogTAG, "删除Item:{0} 失败,error:{1}", tvNode.Name, result.Message);
                }
            }

            dsClient.Close();

            s_completed(hasError, progress, total);
            s_isRunning = false;
            s_thread = null;
        }
    }
}
